// sanya singh

#include <stdlib.h>
#include <stdio.h>
// declared the array and its size globally

int *array;
int n;

/////

int menu()
{
	int choice;
	printf("\t\t******LIST Implementation Using Array******\n");
	printf("\t0. Append\n\t1. Display\n\t2. Insert\n\t3. count\n\t4. Delete\n\t5. reverse\n\t6. index\n\t7. Sort\n\t8. pop\n\t9  Extend\n\t10.  Exit\n\t\tEnter your choice : ");
	scanf("%d", &choice);
	
	return choice;
}
void append(int *p,int num)
{  p=realloc(p,(n+1)*sizeof(int));
   p[n]=num;
   n=n+1;
}

// insert element into list at index i
void insert(int i,int elm)
{ int j;
  n=n+1;
  for(j=i;j<=n;j++)
  { array[j+1]=array[j];
   }
   array[i]=elm;
}


// display the list
void display(int *p)
{   int i;
    printf("\n the array is=>");    
    for (i=0; i<n; i++) 
    {
          printf("%d\t",p[i]);
    }
}
//count
int count(int num)
{
        int count,i;      
        int flag=0;
        count=0;
        
        for (i=0;i<n;i++)
        {
                if (array[i]==num)
                {       flag=1;
                        count++;
                }
        }       
	if(flag==0)
	printf("\n number not in list!!!");

        return count;


   
}
// delete

int delete(int elm)

{ int i,j;
  for(i=0;i<n;i++)

  { if(array[i]==elm)

    { for(j=i; j<n ;j++)
	array[j]= array[j+1];
	  n--;
     }
   }

  return array;
}


void reverse ()
{       int j,temp,q;
        q=n-1;
        for(j=0;j<=n/2;j++)
        {  temp=array[j];
           array[j]=array[q];
           array[q]=temp;
           q--;
         }
}
// returns index of first occurence of x
int index_num(int x)
{ int c=0,i;
  for(i=0;i<n;i++)
  {  if(array[i]==x)
      { return i;
      }
  }
}
void sort()
{
int i,j,temp;

for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(array[j]>array[i])
			{
				temp=array[i];
				array[i]=array[j];
				array[j]=temp;
			}
		}
	}
}
void pop(int i)
{ 
  if((i>=n)&&(i<0))
  { printf("\n element does not exist!!!");
    return;
   }
  int elm=array[i];
  delete(elm);
  printf("element is:%d",elm);
}

int extend()
{       int b[100];
	int num_elements,i;
        printf("\n enter the number elements in new array:");
        scanf("%d",&num_elements);
        printf(" enter the elements:\n");
        for (i=0;i<num_elements;i++)
	{       printf("element %d=",i+1);
		scanf("%d",&b[i]);
	}
	array=realloc(array,(n+num_elements)*sizeof(int*));
	for(i=0;i<num_elements;i++)
	{ 
		
		array[n]=b[i];
		n++;
	}
	return n;
}




void main()
{       array=(int*)malloc(n*sizeof(int));
	int choice;
	while(1)
	{
		choice = menu();
		switch (choice)
		{
		    case 0:     {  int e;
                    		   printf("\n enter the element to append :");
                    		   scanf("%d",&e);
                    		   append(array,e);
                    		   break;
                                 }

     		    case 1:      { printf("\n the elements in the list are:");
                                   display(array);
                                 break;
                        
		                 }
                    case 2:      { int i,num;
				    printf("enter the element:");
				    scanf("%d",&num);
				    printf("enter the location:");
				    scanf("%d",&i);
				    insert(i,num);
				    break;
                   		}
                    case 3:     { int num,p;
                                  printf("\n enter the number to be counted:");
                                  scanf("%d",&num);
                                  p=count(num);
                                  printf("number of times %d appears is=%d",num,p);
                                  break;
                                 }			
                    case 4:      { int num, e;
                                   printf("enter the element to be deleted:");
			           scanf("%d", &num);	
				   delete(num);
	                           break;
	                         }
                    case 5:    { reverse();
                                 
                                 break;
                                }
                    case 6:    {int p,e;
                               printf("\n enter the number whose index you want to find:");
                               scanf("%d",&p);
                               e=index_num(p);
                               printf("\n index of %d is = %d",p,e);
                               break;
                                }
                    case 7:   {  sort();
                                  break;
                               }
                    case 8:  { int i;
                               printf("\n enter the index number:");
                               scanf("%d",&i);
                               pop(i);
                               break;
                             }
                    case 9:     extend();
                                break; 
                    case 10:    exit(0);
                    default:    printf("\n invalid choice:");

	         }
         }

}
// end!!!
